package com.zhou.bjgl.bjgl.util.format;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateFomatUtil {

	public static int getMinute(Date before,Date after){
		long day = 0;  
        long hour = 0;  
        long min = 0;  
        long sec = 0;
        long time1 = before.getTime();  
		long time2 = after.getTime();  
		long diff ;  
		diff = time2 - time1;
		day = diff / (24 * 60 * 60 * 1000);  
		hour = (diff / (60 * 60 * 1000) - day * 24);  
		min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
		sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
		System.out.println(day + "天" + hour + "小时" + min + "分" + sec + "秒"  );
		return new Long(day*24*60+hour*60+min).intValue();
	}
	public static String getCurrentTime(){
		Date d=new Date();
		return formatTime(d);
	}
	public static String formatIdPre(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmm");
		return sdf.format(date);
	}
	public static String format(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	public static String formatOnlyTime(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
		return sdf.format(date);
	}
	public static String formatTransferConfig(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}
	public static Date parseDate(String date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date returnDate;
		try {
			returnDate = (Date) sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return new Date();
		}
		return returnDate;
	}
	public static String format(Timestamp timestamp){
		if(timestamp==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(timestamp);
	}

	public static String formatTime(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	public static String formatTimeNoSs(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}

	public static String formatTimeChina(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日HH点");
		return sdf.format(date);
	}
	public static Timestamp parse(String timestamp) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return (Timestamp) sdf.parse(timestamp);
	}
	public static Date parseNoSs(String dateString) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return  sdf.parse(dateString);
	}
	public static Date parseDateTime(String timestamp) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(timestamp);
		} catch (ParseException e) {
			return null;
		}
	}
	public static int getXingQiByDate(String date) throws ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date d=sdf.parse(date);
		return d.getDay();
	}

	public static List<String> getDateString() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date afterDate = new Date(date.getTime() + 65000);
		List<String> stringList = new ArrayList<>();
		stringList.add(sdf.format(date));
		stringList.add(sdf.format(afterDate));
		return stringList;
	}
	public static String formatPre(Date date){
		if(date==null)
			return "";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
		return sdf.format(date);
	}
	public static Date getTheDateBefore(Date date,int d){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,d);
		return calendar.getTime();
	}
	public static String getTheDateStrBefore(Date date,int d){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,d);
		return format(calendar.getTime());
	}
	
	public static Date getTheMinBefore(Date date,int d){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE,d);
		return calendar.getTime();
	}
	public static String getTheMinStrBefore(Date date,int d){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE,d);
		return format(calendar.getTime());
	}
	/**
	 * 获取本月最后一天日期
	 * @return
	 */
	public static Date getTheMonthLastDate(){
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0); 
		String lastDate=format(cale.getTime());
		lastDate+=" 23:59:59";
		try {
			Date d=parseDateTime(lastDate);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cale.getTime();
	}
	/**
	 * 获取本月第一天日期
	 * @return
	 */
	public static Date getTheMonthFirstDate(){
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1); 
		String lastDate=format(cale.getTime());
		lastDate+=" 00:00:00";
		try {
			Date d=parseDateTime(lastDate);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cale.getTime();
	}
	/**
	 * 获取上月最后一天日期
	 * @return
	 */
	public static Date getPreMonthLastDate(){
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 0); 
		String lastDate=format(cale.getTime());
		lastDate+=" 23:59:59";
		try {
			Date d=parseDateTime(lastDate);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cale.getTime();
	}
	/**
	 * 获取上月第一天日期
	 * @return
	 */
	public static Date getPreMonthFirstDate(){
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, -1);
		cale.set(Calendar.DAY_OF_MONTH, 1); 
		String lastDate=format(cale.getTime());
		lastDate+=" 00:00:00";
		try {
			Date d=parseDateTime(lastDate);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cale.getTime();
	}
	/**
	 * 获取该时间当月第一天
	 * @return
	 */
	public static Date getTheDayFirstDate(Date date){
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1); 
		String lastDate=format(cale.getTime());
		lastDate+=" 00:00:00";
		try {
			Date d=parseDateTime(lastDate);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cale.getTime();
	}
	/**
	 * 获取该时间当月第一天
	 * @return
	 */
	public static Date getTheDayLastDate(Date date){
		Calendar cale = Calendar.getInstance();
		cale.setTime(date);
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0); 
		String lastDate=format(cale.getTime());
		lastDate+=" 23:59:59";
		try {
			Date d=parseDateTime(lastDate);
			return d;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cale.getTime();
	}
	public static void main(String[] args) {
		Date date=new Date();
		for(int i=1;i<=7;i++) {
			System.out.println(getTheDateStrBefore(date, i));
		}
	}
}
